import type {
  Novel,
  NovelCategory,
  NovelChapter,
  PaginatedResponse,
  NovelListParams,
  ChapterListParams,
  AddFavoriteRequest,
  AddReadingHistoryRequest,
  CreateCommentRequest,
  UpdateCommentRequest,
  FavoriteParams,
  ReadingHistoryParams,
  CommentListParams,
  UserStats,
  ReadingPreferences,
  RecommendedNovelsParams,
  Bookshelf,
  CreateBookshelfRequest,
  UpdateBookshelfRequest,
  BookshelfParams,
  MoveFavoritesRequest,
  FavoriteParamsExtended,
  UserFavoriteExtended,
} from './types/novel'
import { http } from '@/http/http'

// ==================== 小说分类 ====================

/**
 * 获取小说分类列表
 */
export function getNovelCategories() {
  return http.get<NovelCategory[]>('/novels/categories')
}

// ==================== 小说管理 ====================

/**
 * 获取小说列表
 */
export function getNovelList(params: NovelListParams = {}) {
  return http.get<PaginatedResponse<Novel>>('/novels', params)
}

/**
 * 获取小说详情
 */
export function getNovelDetail(id: number) {
  return http.get<Novel>(`/novels/${id}`)
}

/**
 * 搜索小说
 */
export function searchNovels(params: { keyword: string; page?: number; limit?: number }) {
  return http.get<PaginatedResponse<Novel>>('/novels/search', params)
}

/**
 * 获取推荐小说
 */
export function getRecommendedNovels(params: { limit?: number; categoryId?: number } = {}) {
  return http.get<Novel[]>('/novels/recommended', params)
}

/**
 * 获取热门小说
 */
export function getPopularNovels(params: { limit?: number; categoryId?: number } = {}) {
  return http.get<Novel[]>('/novels/popular', params)
}

// ==================== 章节管理 ====================

/**
 * 获取小说章节列表
 */
export function getChapterList(novelId: number, params: ChapterListParams = {}) {
  return http.get<PaginatedResponse<NovelChapter>>(`/novels/${novelId}/chapters`, params)
}

/**
 * 获取章节详情
 */
export function getChapterDetail(novelId: number, chapterId: number) {
  return http.get<NovelChapter>(`/novels/${novelId}/chapters/${chapterId}`)
}

// ==================== 用户交互 - 收藏 ====================

/**
 * 添加小说到收藏
 */
export function addFavorite(data: AddFavoriteRequest) {
  return http.post<any>('/novels/interaction/favorites', data)
}

/**
 * 取消收藏小说
 */
export function removeFavorite(novelId: number) {
  return http.delete<void>(`/novels/interaction/favorites/${novelId}`)
}

/**
 * 检查是否已收藏
 */
export function checkFavorite(novelId: number) {
  return http.get<{ isFavorited: boolean }>(`/novels/interaction/favorites/check/${novelId}`)
}

/**
 * 获取用户收藏列表
 */
export function getUserFavorites(params: FavoriteParamsExtended = {}) {
  return http.get<PaginatedResponse<UserFavoriteExtended>>('/novels/interaction/favorites', params)
}

/**
 * 批量删除收藏
 */
export function batchRemoveFavorites(novelIds: number[]) {
  return http.delete<{ success: number; failed: number }>('/novels/interaction/favorites/batch', { novelIds })
}

// ==================== 用户交互 - 阅读历史 ====================

/**
 * 添加阅读历史
 */
export function addReadingHistory(data: AddReadingHistoryRequest) {
  return http.post<UserReadingHistory>('/novels/interaction/reading-history', data)
}

/**
 * 获取阅读历史
 */
export function getReadingHistory(params: ReadingHistoryParams = {}) {
  return http.get<PaginatedResponse<UserReadingHistory>>('/novels/interaction/reading-history', params)
}

/**
 * 获取小说阅读进度
 */
export function getReadingProgress(novelId: number) {
  return http.get<{ progress: number }>(`/novels/interaction/reading-progress/${novelId}`)
}

/**
 * 清空阅读历史
 */
export function clearReadingHistory() {
  return http.delete<void>('/novels/interaction/reading-history/clear')
}

// ==================== 用户交互 - 评论 ====================

/**
 * 创建评论
 */
export function createComment(data: CreateCommentRequest) {
  return http.post<Comment>('/novels/interaction/comments', data)
}

/**
 * 获取评论列表
 */
export function getComments(params: CommentListParams) {
  return http.get<PaginatedResponse<Comment>>('/novels/interaction/comments', params)
}

/**
 * 获取评论详情
 */
export function getCommentDetail(commentId: number) {
  return http.get<Comment>(`/novels/interaction/comments/${commentId}`)
}

/**
 * 更新评论
 */
export function updateComment(commentId: number, data: UpdateCommentRequest) {
  return http.post<Comment>(`/novels/interaction/comments/${commentId}/update`, data)
}

/**
 * 删除评论
 */
export function deleteComment(commentId: number) {
  return http.delete<void>(`/novels/interaction/comments/${commentId}`)
}

/**
 * 点赞评论
 */
export function likeComment(commentId: number) {
  return http.post<Comment>(`/novels/interaction/comments/${commentId}/like`)
}

/**
 * 取消点赞评论
 */
export function unlikeComment(commentId: number) {
  return http.delete<Comment>(`/novels/interaction/comments/${commentId}/like`)
}

/**
 * 获取用户的评论列表
 */
export function getUserComments(params: { page?: number; limit?: number } = {}) {
  return http.get<PaginatedResponse<Comment>>('/novels/interaction/user-comments', params)
}

// ==================== 用户档案 ====================

/**
 * 获取用户基本信息
 */
export function getUserProfile() {
  return http.get<any>('/user/profile')
}

/**
 * 更新用户基本信息
 */
export function updateUserProfile(data: any) {
  return http.put<any>('/user/profile', data)
}

/**
 * 修改密码
 */
export function changePassword(data: { oldPassword: string; newPassword: string }) {
  return http.put<any>('/user/profile/password', data)
}

/**
 * 上传头像
 */
export function uploadAvatar(file: File) {
  const formData = new FormData()
  formData.append('avatar', file)
  return http.post<{ avatarUrl: string }>('/user/profile/avatar', formData)
}

/**
 * 获取用户统计数据
 */
export function getUserStats() {
  return http.get<UserStats>('/user/profile/stats')
}

/**
 * 获取用户阅读统计
 */
export function getReadingStats() {
  return http.get<any>('/user/profile/reading-stats')
}

/**
 * 获取用户活动时间线
 */
export function getActivityTimeline(params: { limit?: number } = {}) {
  return http.get<any[]>('/user/profile/activity-timeline', params)
}

/**
 * 获取用户个人推荐小说
 */
export function getUserRecommendedNovels(params: RecommendedNovelsParams = {}) {
  return http.get<Novel[]>('/user/profile/recommendations', params)
}

/**
 * 获取用户阅读偏好分析
 */
export function getReadingPreferences() {
  return http.get<ReadingPreferences>('/user/profile/reading-preferences')
}

/**
 * 更新阅读偏好设置
 */
export function updateReadingPreferences(data: any) {
  return http.put<any>('/user/profile/reading-preferences', data)
}

/**
 * 获取用户最近阅读
 */
export function getRecentReading(params: { limit?: number } = {}) {
  return http.get<UserReadingHistory[]>('/user/profile/recent-reading', params)
}

/**
 * 获取用户收藏分类统计
 */
export function getFavoriteCategories() {
  return http.get<any[]>('/user/profile/favorite-categories')
}

/**
 * 获取用户阅读时长统计
 */
export function getReadingTimeStats(params: { period?: string } = {}) {
  return http.get<any>('/user/profile/reading-time-stats', params)
}

/**
 * 导出用户数据
 */
export function exportUserData() {
  return http.get<any>('/user/profile/export')
}

// ==================== 书架管理 ====================

/**
 * 创建书架
 */
export function createBookshelf(data: CreateBookshelfRequest) {
  return http.post<Bookshelf>('/novels/interaction/bookshelves', data)
}

/**
 * 获取用户书架列表
 */
export function getUserBookshelves(params: BookshelfParams = {}) {
  return http.get<PaginatedResponse<Bookshelf>>('/novels/interaction/bookshelves', params)
}

/**
 * 更新书架
 */
export function updateBookshelf(bookshelfId: number, data: UpdateBookshelfRequest) {
  return http.post<Bookshelf>(`/novels/interaction/bookshelves/${bookshelfId}/update`, data)
}

/**
 * 删除书架
 */
export function deleteBookshelf(bookshelfId: number) {
  return http.delete<void>(`/novels/interaction/bookshelves/${bookshelfId}`)
}

/**
 * 批量移动收藏到指定书架
 */
export function moveFavoritesToBookshelf(data: MoveFavoritesRequest) {
  return http.post<{ success: number; failed: number }>('/novels/interaction/bookshelves/move-favorites', data)
}